/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.PubAttach;
import com.thinkgem.jeesite.modules.pub.service.PubAffectService;
import com.thinkgem.jeesite.modules.pub.service.PubAttachService;
import com.thinkgem.jeesite.modules.pub.service.PubUserService;
import com.thinkgem.jeesite.modules.pub.service.PubWelfareService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/pubAttach")
public class PubAttachController extends BaseController {

	@Autowired
	private PubAttachService pubAttachService;
	
	@Autowired
	private PubAffectService pubAffectService;
	
	@Autowired
	private PubWelfareService pubWelfareService;
	
	@ModelAttribute
	public PubAttach get(@RequestParam(required=false) String id) {
		PubAttach entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubAttachService.get(id);
		}
		if (entity == null){
			entity = new PubAttach();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:pubAttach:view")
	@RequestMapping(value = {"list", ""})
	public String list(PubAttach pubAttach, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PubAttach> page = pubAttachService.findPage(new Page<PubAttach>(request, response), pubAttach); 
		model.addAttribute("page", page);
		return "modules/pub/pubAttachList";
	}

	@RequiresPermissions("pub:pubAttach:view")
	@RequestMapping(value = "form")
	public String form(PubAttach pubAttach, Model model) {
		model.addAttribute("pubAttach", pubAttach);
		return "modules/pub/pubAttachForm";
	}

	@RequiresPermissions("pub:pubAttach:edit")
	@RequestMapping(value = "save")
	public String save(PubAttach pubAttach, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pubAttach)){
			return form(pubAttach, model);
		}
		pubAttachService.save(pubAttach);
		addMessage(redirectAttributes, "保存评论管理成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubAttach/?repage";
	}
	
	@RequiresPermissions("pub:pubAttach:edit")
	@RequestMapping(value = "delete")
	public String delete(PubAttach pubAttach, RedirectAttributes redirectAttributes) {
		pubAttachService.delete(pubAttach);
		addMessage(redirectAttributes, "删除评论管理成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubAttach/?repage";
	}
	
	@RequestMapping(value="wxlike")
	public String wxlike(HttpServletRequest request, HttpServletResponse response,String userid,String articleid,String type,String arttype,String articlename) {
		String id = UUID.randomUUID().toString().replace("-", ""); 
		int result = pubAttachService.updatelike(id,userid,articleid,type,arttype,articlename);
		if("1".equals(arttype)) {
			pubAffectService.updatelike(articleid,type);
		}else {
			pubWelfareService.updatelike(articleid,type);
		}
		return renderString(response, result);
	}
	
	@RequestMapping(value="colList")
	public String colList(HttpServletRequest request, HttpServletResponse response,String userid) {
		List<Object[]> result = pubAttachService.colList(userid);
		return renderString(response, result);
		
	}
	
	@RequestMapping(value="getlikeflag")
	public String getlikeflag(HttpServletRequest request, HttpServletResponse response,String userid,String articleid) {
		PubAttach entity = null;
		entity = pubAttachService.getlikeflag(userid,articleid);
		
		if (entity == null){
			entity = new PubAttach();
		}
		return renderString(response, entity);
	}
	@RequestMapping(value="getcolflag")
	public String getcolflag(HttpServletRequest request, HttpServletResponse response,String userid,String articleid) {
		PubAttach entity = null;
		entity = pubAttachService.getcolflag(userid,articleid);
		if (entity == null){
			entity = new PubAttach();
		}
		return renderString(response, entity);
	}
	
	@RequestMapping(value = "commentslist")
	public String commentslist(PubAttach pubAttach, HttpServletRequest request, HttpServletResponse response, String articleid) {
		List<Object[]> page = pubAttachService.findComment(articleid); 
		
		return renderString(response, page);
	}
	@RequestMapping(value = "savecomment")
	public String savecomment(HttpServletRequest request, HttpServletResponse response, String articleid,String userid,String type,String arttype,String comment,String articlename) {
		String id = UUID.randomUUID().toString().replace("-", "");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String commenttime = df.format(new Date());
		int result = pubAttachService.savecomment(id,articleid,userid,type,arttype,comment,commenttime,articlename);
		
		return renderString(response, result);
		
	}
	
	

}